package com.ruipeng.shengyapo.controller;

import com.ruipeng.shengyapo.comm.Page;
import com.ruipeng.shengyapo.comm.ResponseData;
import com.ruipeng.shengyapo.pay.alipay.util.AlipayNotify;
import com.ruipeng.shengyapo.service.OrderService;
import com.ruipeng.shengyapo.service.PayService;
import com.ruipeng.shengyapo.service.PayService;
import com.ruipeng.shengyapo.service.UserService;
import com.ruipeng.shengyapo.utils.ContentUtils;
import com.ruipeng.shengyapo.utils.ObjUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by tu_pei on 17/5/28.
 * 支付相关
 */

@Controller
@RequestMapping("pay")
public class PayController {

    @Autowired
    private PayService payService;

    /**
     * 获取用户钱包明细
     * @param userId
     * @return
     */
    @RequestMapping(value = "/{userId}/info" , method = RequestMethod.POST)
    @ResponseBody
    public ResponseData getUserPayByUserId(@PathVariable int userId){
        ResponseData data = new ResponseData();
        //Page p = Page.newBuilder(page,limit);

        //data.setResults(p);
        data.setResults(payService.getUserPayPageList(userId));
        data.setState(ContentUtils.COMM_STATE_TRUE);
        return data;
    }

    /**
     * 交押金
     * @param userId
     * @param type
     * @param payOrderId
     * @param money
     * @return
     */
    @RequestMapping(value = "/deposit/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData depositAdd(@PathVariable int userId,String payOrderId,String type,int money){
        ResponseData data = new ResponseData();

        if(ObjUtils.isNotEmpty(type) || ObjUtils.isNotEmpty(money) || ObjUtils.isNotEmpty(payOrderId)){
            System.out.print("type="+type+",money="+money+",payOrderId="+payOrderId);
            data.setMsg("参数为空");
            data.setState(ContentUtils.COMM_STATE_FALSE);
            return data;
        }else{
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("userId",userId);
            params.put("payOrderId",payOrderId);
            params.put("type",type);
            params.put("money",money);
            payService.depositAdd(params);
            data.setState(ContentUtils.COMM_STATE_TRUE);
            return data;
        }
    }

    /**
     * 退押金
     * @param userId
     * @return
     */
    @RequestMapping(value = "/deposit/back" , method = RequestMethod.POST)
    @ResponseBody
    public ResponseData depositBack(@PathVariable int userId){
        ResponseData data = new ResponseData();
        Map<String,Object> params = new HashMap<String,Object>();
        Map<String,Object> depositMap = new HashMap<String,Object>();
        if(ObjUtils.isNotEmpty(depositMap)){
            params.put("userId",userId);
            payService.depositBack(params);
            data.setMsg("没有押金可退");
            data.setState(ContentUtils.COMM_STATE_FALSE);
            return data;
        }else{
            params.put("userId",userId);
            payService.depositBack(params);
            data.setState(ContentUtils.COMM_STATE_TRUE);
            return data;
        }
    }

    /**
     * 优惠券列表
     * @param userId
     * @return
     */
    @RequestMapping(value = "/coupon/{userId}",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData getCoupon(@PathVariable int userId,int page,int limit){
        ResponseData data = new ResponseData();

        Page p = Page.newBuilder(page,limit);

        p = payService.getCouponPageList(p,userId);
        data.setResults(p);
        data.setState(ContentUtils.COMM_STATE_FALSE);
        return data;
    }

    /**
     * 使用优惠券
     * @param userId
     * @return
     */
    @RequestMapping(value = "/coupon/{userId}/{couponCode}" , method = RequestMethod.POST)
    @ResponseBody
    public ResponseData updateUserCoupon(@PathVariable int userId,@PathVariable String couponCode){
        ResponseData data = new ResponseData();
        if(ObjUtils.isNotEmpty(couponCode)){
            data.setMsg("请选择优惠券");
            data.setState(ContentUtils.COMM_STATE_TRUE);
            return data;
        }else {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("userId",userId);
            params.put("couponCode",couponCode);

            payService.updateUserCoupon(params);
            return  data;
        }
    }

    /**
     * 微信支付回调商户地址
     */
    @RequestMapping("/back/wxPay" )
    @ResponseBody
    public void backWxPay(){
    }

}
